package com.youxin.foundation.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.ObjectUtils.Null;

import net.sf.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * JSON转换类
 * 
 * @author yingde.cao
 *
 */
public class JsonUtils {

	/**
	 * java Map转换为Google Json
	 * 
	 * @param map
	 * @return Gson JsonObject
	 */
	public static JsonObject mapToGoogleJson(Map<String, String> map) {
		JsonObject jsonObject = new JsonObject();
		for (Map.Entry<String, String> entry : map.entrySet()) {
			jsonObject.addProperty(entry.getKey(), entry.getValue());
		}
		return jsonObject;
	}

	public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) throws Exception {
		if (map == null)
			return null;
		try {
			T obj = beanClass.newInstance();
			org.apache.commons.beanutils.BeanUtils.populate(obj, map);
			return obj;
		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * Map 转换为net.sf.json.JSONObject
	 * 
	 * @param map
	 * @return net.sf.json.JSONObject
	 */
	public static JSONObject mapToSfJson(Map<String, String> map) {
		JSONObject jsonObject = JSONObject.fromObject(map);
		return jsonObject;
	}

	public static String mapToJsonString(Map<String, String> map) {
		JSONObject jsonObject = mapToSfJson(map);
		return jsonObject.toString();
	}

	/**
	 * Object转换为net.sf.json.JSONObject
	 * 
	 * @param object
	 * @return net.sf.json.JSONObject
	 */
	public static JSONObject objectToSfJson(Object object) {
		JSONObject jsonObject = JSONObject.fromObject(object);
		return jsonObject;
	}

	/**
	 * Object 转换为Java Map
	 * 
	 * @param object
	 * @return Map<String, String>
	 */
	public static Map<String, String> objectToMap(Object object) {
		JSONObject jsonObject = objectToSfJson(object);
		return jsonToMap(jsonObject);
	}

	public static Map<String, Object> objectToMapObject(Object object) {
		JSONObject jsonObject = objectToSfJson(object);
		return jsonToMapObject(jsonObject);
	}

	/**
	 * net.sf.json 转换为 Map,目前不支持局部类
	 * 
	 * @param jsonObject
	 * @return Map<String, String>
	 */
	public static Map<String, String> jsonToMap(JSONObject jsonObject) {
		Map<String, String> data = new HashMap<String, String>(10);
		Iterator<?> it = jsonObject.keys();
		// 遍历jsonObject数据，添加到Map对象
		while (it.hasNext()) {
			String key = String.valueOf(it.next());
			Object object = jsonObject.get(key);
			if (object.equals(null)) {
				continue;
			}
			String value = String.valueOf(object);
			data.put(key, value);
		}
		return data;
	}

	public static Map<String, Object> jsonToMapObject(JSONObject jsonObject) {
		Map<String, Object> data = new HashMap<String, Object>(10);
		Iterator<?> it = jsonObject.keys();
		// 遍历jsonObject数据，添加到Map对象
		while (it.hasNext()) {
			String key = String.valueOf(it.next());
			Object object = jsonObject.get(key);
			if (object.equals(null)) {
				continue;
			}
			data.put(key, object);
		}
		return data;
	}

	/**
	 * 字符串转换为List<E>
	 * 
	 * @param json
	 *            字符串
	 * @param clz
	 *            class
	 * @return List<E>
	 */
	public static <E> List<E> stringToList(String json, Class<E> clz) {
		return com.alibaba.fastjson.JSON.parseArray(json, clz);
	}

	/**
	 * 字符串转换为Java Object
	 * 
	 * @param json
	 * @param clz
	 * @return Object
	 */
	public static Object stringToObject(String json, Class<?> clz) {
		return com.alibaba.fastjson.JSON.parseObject(json, clz);
	}

	@SuppressWarnings("unchecked")
	public static Map<String, String> stringToMap(String json) {
		if (StringUtils.isEmpty(json)) {
			return null;
		}
		return JSON.parseObject(json, Map.class);
	}

	/**
	 * 对象转换为JSON字符串
	 * 
	 * @param object
	 * @return Json String
	 */
	public static String objectToJsonString(Object object) {
		return com.alibaba.fastjson.JSON.toJSONString(object);
	}

	public static String objectToFasterxmlJsonString(Object object) {
		if (object == null) {
			return "";
		}
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

			String json = mapper.writeValueAsString(object);
			return json;
		} catch (JsonProcessingException e) {
			return "";
		}
	}

	public static <T> T stringToFasterxmlObject(String jsonString, Class<T> cls) {
		return stringToFasterxmlObject(jsonString, cls, false);
	}

	public static <T> T stringToFasterxmlObject(String jsonString, Class<T> cls, boolean failOnUnKnowProperties) {
		try {
			if(jsonString==null)
			{
				return null;
			}
			ObjectMapper mapper = new ObjectMapper();
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnKnowProperties);
			T obj = (T) mapper.readValue(jsonString, cls);
			return obj;
		} catch (Exception e) {
			return null;
		}
	}

	public static <T> T stringToFasterxmlList(String jsonStr, TypeReference<T> valueTypeRef) {
		return stringToFasterxmlList(jsonStr, valueTypeRef, false);
	}

	/**
	 * json数组转List List<UserBean> jsonToUserBeans = readValue(listToJson, new
	 * TypeReference<List<UserBean>>() {
	 * 
	 * @param jsonStr
	 * @param valueTypeRef
	 * @return
	 */
	public static <T> T stringToFasterxmlList(String jsonStr, TypeReference<T> valueTypeRef,
			boolean failOnUnKnowProperties) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnKnowProperties);
			return mapper.readValue(jsonStr, valueTypeRef);
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 字符串转换为json
	 * 
	 * @param json
	 * @return JSONObject
	 */
	public static com.alibaba.fastjson.JSONObject stringToJsonObject(String json) {
		return com.alibaba.fastjson.JSON.parseObject(json);
	}

	/**
	 * Json String to Map
	 * 
	 * @param json
	 *            string
	 * @return Map
	 */
	@SuppressWarnings("serial")
	public static <T> Map<String, T> stringToMapGson(String json) {
		return new Gson().fromJson(json, new TypeToken<HashMap<String, T>>() {
		}.getType());
	}

	/**
	 * 复杂类型：Json转化为bean<br>
	 * 用法示例：<br>
	 * Map<String, Class> classMap = new HashMap<String, Class>();
	 * classMap.put("list", ChildBean.class); //指定复杂类型属性的映射关系，可以使多个放到map中<br>
	 * Person person=(Person)JsonUtil.jsonString2Object(str2, Person.class,
	 * classMap);<br>
	 * 
	 * @param jsonString
	 * @param pojoCalss
	 * @return
	 */
	public static Object jsonString2Object(String jsonString, Class<?> pojoCalss, Map<String, Class<?>> classMap) {
		JSONObject jobj = JSONObject.fromObject(jsonString);

		return JSONObject.toBean(jobj, pojoCalss, classMap);
	}
	
	    

	// public static String obj2JsonString(Object obj) {
	// return JSON.toJSONString(obj);
	// }

}
